package gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

import java.util.ArrayList;
import java.util.Collections;

@Configuration
public class MallCorsConfiguration {

    //@Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 跨域会先发送options预检请求确认服务器是否允许跨域
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许所有请求头
        corsConfiguration.addAllowedHeader("*");
        // 允许所有请求类型
        corsConfiguration.addAllowedMethod("*");
        // 允许所有的请求来源
        corsConfiguration.addAllowedOrigin("*");
        // 允许携带cookie的跨域
        corsConfiguration.setAllowCredentials(true);
        ArrayList<String> strings = new ArrayList<>();
        strings.add("*");
        corsConfiguration.setAllowedOriginPatterns(strings);
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
